package com.hsh.controller;

import com.hsh.annotations.annotation.OperationLog;
import com.hsh.annotations.annotation.Retry;
import com.hsh.config.redis.RedisUtil;
import com.hsh.entity.DbDepartment;
import com.hsh.entity.DbUser;
import com.hsh.persist.BaseController;
import com.hsh.service.IDbDepartmentService;
import com.hsh.service.IDbUserService;
import com.hsh.tools.DateUtils2;
import com.hsh.tools.StrUtils;
import com.hsh.tools.convert.DeptConvert;
import com.hsh.tools.convert.DeptTreeBuilder;
import com.hsh.tools.handler.BusinessException;
import com.hsh.utils.vo.Response;
import com.hsh.vo.result.convert.DeptVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@Api(value = "IndexController", tags = "测试管理器")
@RestController
@RequestMapping("/test")
public class IndexController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());

    public int count = 0;

    @Autowired
    IDbUserService userService;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    IDbDepartmentService departmentService;

    @ApiOperation(value = "good", notes = "good", position = 1)
    @OperationLog(operation = "good", exceptionMessage = "good")
    @GetMapping("/good")
    public Response good() {
        List<DbDepartment> list = departmentService.list();
        List<DeptVO> deptVOS = DeptConvert.converterToMenuNodeVO(list);
        List<DeptVO> build = DeptTreeBuilder.ListToTree(deptVOS);
        return Response.success(build);
    }

    //    @RequiresPermissions("record-apply-for:add")// 按钮权限判定
//    @RequiresRoles("员工") // 角色判定
//    @ApiImplicitParam(name = "Authorization", value = "Authorization", required = true, paramType = "header")
    @ApiOperation(value = "测试删除数据", notes = "测试删除数据", response = TestResult.class)
    @OperationLog(operation = "删除数据表", exceptionMessage = "系统异常")
    @GetMapping("/del/{content}")
    public Response delDb(@PathVariable("content") String content) {
        TestResult result = new TestResult();
        String data = "删除数据表: " + (StrUtils.isEmpty(content) ? "空值" : content);
        result.setContent(data);
        /*
        "msg": "fail",
        "verror": "删除数据表: kong"
         */
        return Response.success(result);
    }

    @ApiOperation(value = "重试测试1", notes = "重试测试2", response = DbUser.class)
    @OperationLog(operation = "重试测试", exceptionMessage = "系统异常")
    @Retry(value = 2, sleep = 500)
    @GetMapping("/retry")
    public Response testRetry() throws Exception {
        //重试的机会
        if (++count <= 3) {
            throw new BusinessException("当前已重试：" + count + "，已进入异常");
        }
        return Response.success(userService.list());
    }

    @ApiOperation(value = "输出输入数据返回", notes = "输出输入数据返回", response = String.class)
    @GetMapping("/get/{str}")
    public Response testRes(@PathVariable(value = "str") String str) throws Exception {
        System.out.println("测试数据~");
        if (str.contains("h")) {
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write("~~~~~~~~~~~~~~黄思海");
            return null;
        }
        return Response.success(str);
    }


    @ApiOperation(value = "测试异常", notes = "测试异常")
    @OperationLog(operation = "测试异常写入", exceptionMessage = "测试异常写入")
    @GetMapping("/ex/{msg}")
    public Response testException(@PathVariable("msg") String msg) {
        if (Objects.equals("fail", msg)) {
            int i = 1 / 0;
            return Response.fail();
        } else if (Objects.equals("success", msg)) {
            return Response.success("测试异常 --> success");
        }
        throw new BusinessException("自定义抛出异常：" + msg);
    }

    @ApiOperation(value = "测试redis CLEAR", notes = "测试redis CLEAR")
    @OperationLog(operation = "测试redis CLEAR", exceptionMessage = "测试redis CLEAR")
    @GetMapping("/redis/clear")
    public Response testRedisClear() {
        return Response.success(redisUtil.clear());
    }


    @ApiOperation(value = "测试redis读", notes = "测试redis读")
    @OperationLog(operation = "测试redis读", exceptionMessage = "测试redis异常")
    @GetMapping("/redis/read/{flag}")
    public Response testRedisRead(@PathVariable("flag") String flag) {
        Object o = redisUtil.get(flag);
        return Response.success(o);
    }

    @ApiOperation(value = "测试redis写", notes = "测试redis写", response = String.class)
    @OperationLog(operation = "测试redis写", exceptionMessage = "测试redis写异常")
    @GetMapping("/redis/write/{flag}/{content}")
    public Response testRedisRead(@PathVariable("flag") String flag, @PathVariable("content") String content) {
        boolean status = redisUtil.set(flag, content, 10);

        return Response.success("redis 写入状态 -->" + status);
    }

}
